﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class Coupon
    {
        public int Add(Chain.Model.Coupon model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Coupon(");
            builder.Append("CouponTitle,CouponType,CouponNumber,CouponPredictNu,CouponEffective,CouponStart,CouponEnd,CouponDayNum,CouponMinMoney,CouponContent,CouponYF,CouponSY)");
            builder.Append(" values (");
            builder.Append("@CouponTitle,@CouponType,@CouponNumber,@CouponPredictNu,@CouponEffective,@CouponStart,@CouponEnd,@CouponDayNum,@CouponMinMoney,@CouponContent,@CouponYF,@CouponSY)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CouponTitle", SqlDbType.NVarChar, 50), new SqlParameter("@CouponType", SqlDbType.Int, 4), new SqlParameter("@CouponNumber", SqlDbType.Float, 8), new SqlParameter("@CouponPredictNu", SqlDbType.Int, 4), new SqlParameter("@CouponEffective", SqlDbType.Int, 4), new SqlParameter("@CouponStart", SqlDbType.DateTime), new SqlParameter("@CouponEnd", SqlDbType.DateTime), new SqlParameter("@CouponDayNum", SqlDbType.Int, 4), new SqlParameter("@CouponMinMoney", SqlDbType.Money, 8), new SqlParameter("@CouponContent", SqlDbType.NVarChar, 500), new SqlParameter("@CouponYF", SqlDbType.Int, 4), new SqlParameter("@CouponSY", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CouponTitle;
            cmdParms[1].Value = model.CouponType;
            cmdParms[2].Value = model.CouponNumber;
            cmdParms[3].Value = model.CouponPredictNu;
            cmdParms[4].Value = model.CouponEffective;
            cmdParms[5].Value = model.CouponStart;
            cmdParms[6].Value = model.CouponEnd;
            cmdParms[7].Value = model.CouponDayNum;
            cmdParms[8].Value = model.CouponMinMoney;
            cmdParms[9].Value = model.CouponContent;
            cmdParms[10].Value = model.CouponYF;
            cmdParms[11].Value = model.CouponSY;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public Chain.Model.Coupon DataRowToModel(DataRow row)
        {
            Chain.Model.Coupon coupon = new Chain.Model.Coupon();
            if (row != null)
            {
                if ((row["ID"] != null) && (row["ID"].ToString() != ""))
                {
                    coupon.ID = int.Parse(row["ID"].ToString());
                }
                if (row["CouponTitle"] != null)
                {
                    coupon.CouponTitle = row["CouponTitle"].ToString();
                }
                if ((row["CouponType"] != null) && (row["CouponType"].ToString() != ""))
                {
                    coupon.CouponType = int.Parse(row["CouponType"].ToString());
                }
                if ((row["CouponNumber"] != null) && (row["CouponNumber"].ToString() != ""))
                {
                    coupon.CouponNumber = decimal.Parse(row["CouponNumber"].ToString());
                }
                if ((row["CouponPredictNu"] != null) && (row["CouponPredictNu"].ToString() != ""))
                {
                    coupon.CouponPredictNu = int.Parse(row["CouponPredictNu"].ToString());
                }
                if ((row["CouponEffective"] != null) && (row["CouponEffective"].ToString() != ""))
                {
                    coupon.CouponEffective = int.Parse(row["CouponEffective"].ToString());
                }
                if ((row["CouponStart"] != null) && (row["CouponStart"].ToString() != ""))
                {
                    coupon.CouponStart = new DateTime?(DateTime.Parse(row["CouponStart"].ToString()));
                }
                if ((row["CouponEnd"] != null) && (row["CouponEnd"].ToString() != ""))
                {
                    coupon.CouponEnd = new DateTime?(DateTime.Parse(row["CouponEnd"].ToString()));
                }
                if ((row["CouponDayNum"] != null) && (row["CouponDayNum"].ToString() != ""))
                {
                    coupon.CouponDayNum = int.Parse(row["CouponDayNum"].ToString());
                }
                if ((row["CouponMinMoney"] != null) && (row["CouponMinMoney"].ToString() != ""))
                {
                    coupon.CouponMinMoney = decimal.Parse(row["CouponMinMoney"].ToString());
                }
                if (row["CouponContent"] != null)
                {
                    coupon.CouponContent = row["CouponContent"].ToString();
                }
                if ((row["CouponYF"] != null) && (row["CouponYF"].ToString() != ""))
                {
                    coupon.CouponYF = int.Parse(row["CouponYF"].ToString());
                }
                if ((row["CouponSY"] != null) && (row["CouponSY"].ToString() != ""))
                {
                    coupon.CouponSY = int.Parse(row["CouponSY"].ToString());
                }
            }
            return coupon;
        }

        public bool Delete(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Coupon ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string IDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Coupon ");
            builder.Append(" where ID in (" + IDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Coupon");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ID,CouponTitle,CouponType,CouponNumber,CouponPredictNu,CouponEffective,CouponStart,CouponEnd,CouponDayNum,CouponMinMoney,CouponContent,CouponYF,CouponSY ");
            builder.Append(" FROM Coupon ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ID,CouponTitle,CouponType,CouponNumber,CouponPredictNu,CouponEffective,CouponStart,CouponEnd,CouponDayNum,CouponMinMoney,CouponContent,CouponYF,CouponSY ");
            builder.Append(" FROM Coupon ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ID desc");
            }
            builder.Append(")AS Row, T.*  from Coupon T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, string[] strWhere, out int resCount)
        {
            string tableName = "Coupon";
            string[] column = new string[] { "* " };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "Coupon");
        }

        public Chain.Model.Coupon GetModel(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ID,CouponTitle,CouponType,CouponNumber,CouponPredictNu,CouponEffective,CouponStart,CouponEnd,CouponDayNum,CouponMinMoney,CouponContent,CouponYF,CouponSY from Coupon ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            new Chain.Model.Coupon();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM Coupon ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.Coupon model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Coupon set ");
            builder.Append("CouponTitle=@CouponTitle,");
            builder.Append("CouponType=@CouponType,");
            builder.Append("CouponNumber=@CouponNumber,");
            builder.Append("CouponPredictNu=@CouponPredictNu,");
            builder.Append("CouponEffective=@CouponEffective,");
            builder.Append("CouponStart=@CouponStart,");
            builder.Append("CouponEnd=@CouponEnd,");
            builder.Append("CouponDayNum=@CouponDayNum,");
            builder.Append("CouponMinMoney=@CouponMinMoney,");
            builder.Append("CouponContent=@CouponContent,");
            builder.Append("CouponYF=@CouponYF,");
            builder.Append("CouponSY=@CouponSY");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CouponTitle", SqlDbType.NVarChar, 50), new SqlParameter("@CouponType", SqlDbType.Int, 4), new SqlParameter("@CouponNumber", SqlDbType.Float, 8), new SqlParameter("@CouponPredictNu", SqlDbType.Int, 4), new SqlParameter("@CouponEffective", SqlDbType.Int, 4), new SqlParameter("@CouponStart", SqlDbType.DateTime), new SqlParameter("@CouponEnd", SqlDbType.DateTime), new SqlParameter("@CouponDayNum", SqlDbType.Int, 4), new SqlParameter("@CouponMinMoney", SqlDbType.Money, 8), new SqlParameter("@CouponContent", SqlDbType.NVarChar, 500), new SqlParameter("@CouponYF", SqlDbType.Int, 4), new SqlParameter("@CouponSY", SqlDbType.Int, 4), new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CouponTitle;
            cmdParms[1].Value = model.CouponType;
            cmdParms[2].Value = model.CouponNumber;
            cmdParms[3].Value = model.CouponPredictNu;
            cmdParms[4].Value = model.CouponEffective;
            cmdParms[5].Value = model.CouponStart;
            cmdParms[6].Value = model.CouponEnd;
            cmdParms[7].Value = model.CouponDayNum;
            cmdParms[8].Value = model.CouponMinMoney;
            cmdParms[9].Value = model.CouponContent;
            cmdParms[10].Value = model.CouponYF;
            cmdParms[11].Value = model.CouponSY;
            cmdParms[12].Value = model.ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

